project=$1
version=$2
host_port=$3
container_port=$4

#2、判断当前服务器是否正在运行容器，sopt rm
#containerId=$(docker ps -a | grep "$project":"$version" | awk '{print $1}')
containerId=$(docker ps -q -f "name=^${project}$")
echo "containerId***************************************$containerId"
if [ "$containerId" ] ; then
   echo "stop container"
   docker stop $containerId
   docker rm -f $containerId
fi
#3、如果目标服务器已经存在当前镜像，删除
#tag=$(docker images -a | grep "$project" | awk '{print $2}')
#echo "tag------------->$tag"
imageId=$(docker images -a "$project":"$version" |grep "$project" | awk '{print $3}')
echo "imageId----------------------------------------->$imageId"
if [ "$imageId" ] ; then
    echo "rmi imageId"
    docker rmi -f $imageId
fi
#4、目标服务器拉取镜像
sudo docker login --username=silentmuh -p=aliyunpassword1qaz crpi-27312djasqecvjte.cn-huhehaote.personal.cr.aliyuncs.com
docker pull crpi-27312djasqecvjte.cn-huhehaote.personal.cr.aliyuncs.com/silentmuh-docker/"$project":"$version"
#5、将拉取的镜像运行成容器
docker run -d --name "$project" -p "${host_port}":"${container_port}" crpi-27312djasqecvjte.cn-huhehaote.personal.cr.aliyuncs.com/silentmuh-docker/"$project":"$version"
docker logout
echo "SUCCESS"